﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;


namespace VelesPresentation
{
    public class CategoryPresenter : Presenter
    {
        ICategoryView view;

        public CategoryPresenter(ICategoryView view)
        {
            this.view = view;
            this.view.AddAttributesToCategory += new VoidEventHandler(view_AddAttributesToCategory);
            this.view.AddCategory += new VoidEventHandler(view_AddCategory);
            this.view.AddSubCategory += new VoidEventHandler(view_AddSubCategory);
            this.view.EditCategory += new VoidEventHandler(view_EditCategory);
            this.view.GetCategories += new VoidEventHandler(view_GetCategories);
            this.view.RemoveAttributesFromCategory += new VoidEventHandler(view_RemoveAttributesFromCategory);
            this.view.RemoveCategory += new VoidEventHandler(view_RemoveCategory);
        }

        void view_RemoveCategory()
        {
            if (view.CurrentCategory != null) service.DeleteCategory(view.CurrentCategory.CategoryID);
        }

        void view_RemoveAttributesFromCategory()
        {
            if(view.SelectedCategoryAttributes  !=null)
            {
                List<int> AttributeIds = view.SelectedCategoryAttributes.Select(x=>x.AttributeID).ToList();
                service.DeleteAttributesFromCategory(view.CurrentCategory.CategoryID, AttributeIds);
            }
        }

        void view_GetCategories()
        {
            view.Categories = client.SelectCategory();
        }

        void view_EditCategory()
        {
            if (view.CurrentCategory != null) service.UpdateCategory(view.CurrentCategory);
        }

        void view_AddSubCategory()
        {
            if (view.CurrentCategory != null) service.InsertSubCategory(view.CurrentCategory);
        }

        void view_AddCategory()
        {
            service.InsertCategory();
        }

        void view_AddAttributesToCategory()
        {
            if (view.SelectedAttributes != null)
            {
                List<int> AttributeIds = view.SelectedAttributes.Select(x => x.AttributeID).ToList();
                service.InsertAttributesInCategory(view.CurrentCategory.CategoryID, AttributeIds);
            }
        }
    }
}
